package com.scau.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.scau.dao.TeacherDao;
import com.scau.pojo.Student;
import com.scau.pojo.Teacher;
import com.scau.service.TeacherService;



@RestController
@RequestMapping("/teacherController")
public class TeacherController{

	@Autowired
	TeacherService teacherService;
	
	@Autowired
	TeacherDao teacherDao;
	
	@RequestMapping("/queryAllTeacher")
	public List<Teacher> query() {
		return teacherService.query();
	}
	
	@RequestMapping("/add")
	public int add(@RequestBody JSONObject jsonObject){
		int result=0;
		try{
			result=teacherService.add(jsonObject);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		return result;
	}

	@RequestMapping("/deleteMulti")
	public int deleteMulti(@RequestBody JSONArray jsonArray) throws Exception{
		int result=0;
		try{
			result=teacherService.delete(jsonArray);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		return result;
	}
	@RequestMapping("/editTeacher")
	public int edit(@RequestBody JSONObject jsonObject){
		int result=0;
		try{
			result=teacherService.update(jsonObject);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		return result;
	}
	
	@RequestMapping("/getTeacherInfo")
    public Map<String,Object> getTeacherInfo(@RequestParam String name) throws Exception{
		Map<String,Object> map=teacherDao.getTeacherByName(name);
		map.put("role", "teacher");
		return map;
		
	}
	
	@RequestMapping("/checkTeacherPwd")
    public Map<String,Object> checkTeacherPwd(@RequestParam String name,@RequestParam String password) throws Exception{
		Map<String,Object> resultMap=new HashMap<>();
    	Map<String,Object> map=teacherDao.getTeacherByName(name);
    	Teacher teacher=(Teacher)map.get("teacher");
    	String oldPwd=teacher.gettPassword();
    	if(oldPwd.equals(password)){
    		resultMap.put("returnCode", 1);
    	}else{
    		resultMap.put("returnCode", 0);
    	}
		return resultMap;
		
	}
}
